/* 
 * Licensed Materials - Property of IBM
 * (c) Copyright IBM Corporation 2010. All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:  Use,
 * duplication or disclosure restricted by GSA ADP Schedule
 * Contract with IBM Corp.
 *
 * This code is released under the terms of the Eclipse Public License
 * version 1.0 which can be found at the top level of this project or
 * or at http://www.eclipse.org/org/documents/epl-v10.php
 */

package com.ibm.mining.model;

import java.io.PrintStream;
import java.util.*;

import com.ibm.mining.MyAssert;

public class ModelClass {
	
	String name;
	Model model;
	Map<String, Property> properties = new HashMap<String, Property>();
	boolean trackHistory = false;
	
	public void setHistoryTracked(boolean trackHistory) {
		this.trackHistory = trackHistory;
	}
	
	public boolean isHistoryTracked() {
		return trackHistory;
	}
	
	public ModelClass(String name, Model model) {
		this.name = name;
		this.model = model;
	}
	
	public String getName() {
		return name;
	}
	
	public List<String> getKeys() {
		List<String> keys = new LinkedList<String>();
		for (Property prop : properties.values()) {
			if (prop.isKey()) {
				keys.add(prop.getName());
			}
		}
		//TODO - we may need to sort the keys alphabetically.
		return keys;
	}

	public Property createProperty(String name) {
		Property prop = new Property(name, this);
		MyAssert.check(!properties.containsKey(name), 
				"Property " + name + " already exists for ModelClass " + this.name);
		properties.put(name, prop);
		return prop;
	}
	
	public Collection<Property> getProperties() {
		return properties.values();
	}
	
	public Property getProperty(String name) {
		if (!properties.containsKey(name)) {
			throw new RuntimeException("ModelClass " + this.name + 
				" does not have requested property " + name);
		}
		return properties.get(name);
	}
	
	public String toString() {
		return "<ModelClass " + name + ">";
	}
	
	public void output(PrintStream out) {output(out, "");}
	
	public void output(PrintStream out, String indent) {
		out.println(indent + "Model Class : " + name);
		for (Property prop : properties.values()) {
			prop.output(out, indent + "  ");
		}
	}
	
	public Model getModel() {
		return model;
	}
	
}
